dataKey = 'thispagekey'
timer = false
interval = 1
chrome.storage.local.get dataKey,(item)->
	if item and item[dataKey]
		obj = item[dataKey]
		sellername = obj.sellername
		if sellername
			url = false
			ips = {}
			result={}
			checkLocation = ->
				reportData=->
					reportData.lastReportTime = reportData.lastReportTime || 0
					now = +new Date()
					if now < reportData.lastReportTime + 10*1000
						return off
					reportData.lastReportTime = now
					#上报数据
					unless $.isEmptyObject result
						cloneResult = {}
						i=8
						for agt_id,res of result
							cloneResult[agt_id] = res
							unless --i
								break
						if i>0 and !$.isEmptyObject ips
							return off
						$.getJSON "#{serverUrlPre}Clientcheck/setIpLocation",sellername:sellername,data:cloneResult,->
							for agt_id of cloneResult
								if result[agt_id]
									delete result[agt_id]
							if $.isEmptyObject ips
								interval = 60*5
								startInterval()
				checkOne = ->
					for agt_id,agt_ip of ips
						$.ajax url+agt_ip,{
							dataType:'text'
							error:(xmlReq, textStatus, errorThrown)->
								$.getJSON "#{serverUrlPre}Clientcheck/report",{sellername:sellername,err:"state is #{textStatus},errorThrown:#{errorThrown}"}
								interval +=3
								startInterval()
							success:(text)->
								#$.getJSON "#{serverUrlPre}Clientcheck/setIpLocation",sellername:sellername,res:text,agt_id:agt_id
								result[agt_id]=text
								reportData()
						}
						delete ips[agt_id]
						break
				if $.isEmptyObject ips
					if !$.isEmptyObject result
						#结果没有上报不能再去获取
						reportData()
						return off
					$.getJSON "#{serverUrlPre}Clientcheck/getIP4checkLocation",sellername:sellername,(data)->
						if false==data
							clearInterval timer
							return off
						if data
							url = data.url
							$.extend ips,data.lists
							checkOne()
							if interval>1
								interval=1
								startInterval()
						else
							interval += 300
							if interval <=900 #有最大间隔时间限制
								startInterval()
				else
					checkOne()
					if interval > 1
						interval=1
						startInterval()
			startInterval = ->
				if timer
					clearInterval timer
				timer = setInterval checkLocation,1*1000*interval
				#alert interval
			#checkLocation()
			startInterval()
